package com.zhou.seckill.controller;

import com.alibaba.fastjson.JSON;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.zhou.seckill.domain.SeckillUser;
import com.zhou.seckill.redis.GoodsKey;
import com.zhou.seckill.result.Result;
import com.zhou.seckill.service.GoodsService;
import com.zhou.seckill.service.RedisService;
import com.zhou.seckill.vo.GoodsDetailVo;
import com.zhou.seckill.vo.GoodsVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhou.seckill.vo.Promos;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.spring4.context.SpringWebContext;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("")
public class GoodsController {

  @Autowired
  private GoodsService goodsService;

  @Autowired
  private RedisService redisService;

  @Autowired
  private ThymeleafViewResolver thymeleafViewResolver;

  @Autowired
  private ApplicationContext applicationContext;

  @RequestMapping(value = "/promos")
  @ResponseBody
  public String list(HttpServletRequest request, HttpServletResponse response,
      SeckillUser user) {
   /* model.addAttribute("user", user);

    String html = redisService.get(GoodsKey.getGoodsList, "", String.class);
    if (!StringUtils.isEmpty(html)) {
      return html;
    }

    List<GoodsVo> goodsList = goodsService.listGoodsVo();
    model.addAttribute("goodsList", goodsList);
    SpringWebContext ctx = new SpringWebContext(request, response,
        request.getServletContext(), request.getLocale(), model.asMap(), applicationContext);
    html = thymeleafViewResolver.getTemplateEngine().process("goods_list", ctx);
    if (!StringUtils.isEmpty(html)) {
      redisService.set(GoodsKey.getGoodsList, "", html);
    }*/
    List<GoodsVo> goodsList = goodsService.listGoodsVo();
    List<Promos> processList = new ArrayList<Promos>();
    for (GoodsVo goodsVo : goodsList) {
      Promos t= new Promos();
      t.setPromoId(goodsVo.getPromosId());
      t.setSkuId(goodsVo.getId());
      t.setNum(goodsVo.getStockCount());
      t.setPrice(goodsVo.getSeckillPrice());
      processList.add(t);
    }
    return JSON.toJSONString(processList);
  }

  @RequestMapping(value = "/to_detail/{goodsId}")
  @ResponseBody
  public String detail2(HttpServletRequest request, HttpServletResponse response, Model model,
      SeckillUser user,
      @PathVariable("goodsId") long goodsId) {
    model.addAttribute("user", user);

    String html = redisService.get(GoodsKey.getGoodsDetail, "" + goodsId, String.class);
    if (!StringUtils.isEmpty(html)) {
      return "/static/goods_list.html";
    }

    GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);
    model.addAttribute("goods", goods);

    long startAt = goods.getStartDate().getTime();
    long endAt = goods.getEndDate().getTime();
    long now = System.currentTimeMillis();

    int seckillStatus = 0, remainSeconds = 0;
    if (now < startAt) {//秒杀还没开始，倒计时
      remainSeconds = (int) ((startAt - now) / 1000);
    } else if (now > endAt) {//秒杀已经结束
      seckillStatus = 2;
      remainSeconds = -1;
    } else {//秒杀进行中
      seckillStatus = 1;
    }
    model.addAttribute("seckillStatus", seckillStatus);
    model.addAttribute("remainSeconds", remainSeconds);

    SpringWebContext ctx = new SpringWebContext(request, response,
        request.getServletContext(), request.getLocale(), model.asMap(), applicationContext);
    html = thymeleafViewResolver.getTemplateEngine().process("goods_detail", ctx);
    if (!StringUtils.isEmpty(html)) {
      redisService.set(GoodsKey.getGoodsDetail, "" + goodsId, html);
    }
    return html;
  }

  @RequestMapping(value = "/detail/{goodsId}")
  @ResponseBody
  public Result<GoodsDetailVo> detail(SeckillUser user,
      @PathVariable("goodsId") long goodsId) {
    GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);

    long startAt = goods.getStartDate().getTime();
    long endAt = goods.getEndDate().getTime();
    long now = System.currentTimeMillis();

    int seckillStatus = 0, remainSeconds = 0;
    if (now < startAt) {//秒杀还没开始，倒计时
      remainSeconds = (int) ((startAt - now) / 1000);
    } else if (now > endAt) {//秒杀已经结束
      remainSeconds = -1;
      seckillStatus = 2;
    } else {//秒杀进行中
      seckillStatus = 1;
    }

    GoodsDetailVo vo = new GoodsDetailVo();
    vo.setGoods(goods);
    vo.setUser(user);
    vo.setRemainSeconds(remainSeconds);
    vo.setSeckillStatus(seckillStatus);
    return Result.success(vo);
  }
}
